package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.templates.L1Fishing;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FishingTable {
  public static final Log _log = LogFactory.getLog(FishingTable.class);
  
  private static final Map<Integer, L1Fishing> _fishingMap = new HashMap<>();
  
  private static Random _random = new Random();
  
  private static FishingTable _instance;
  
  public static FishingTable get() {
    if (_instance == null)
      _instance = new FishingTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `server_fishing`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int key = rs.getInt("itemid");
        int random = rs.getInt("random");
        int fishingpole = rs.getInt("fishingpole");
        int fishingpole2 = rs.getInt("fishingpole2");
        if (ItemTable.get().getTemplate(key) == null) {
          _log.error("渔获资料错误: 没有这个编号的道具:" + key);
          delete(key);
          continue;
        } 
        if (fishingpole <= 0 && fishingpole2 <= 0)
          continue; 
        L1Fishing value = new L1Fishing();
        value.set_itemid(key);
        value.set_random(random);
        value.set_fishingpole(fishingpole);
        value.set_fishingpole2(fishingpole2);
        _fishingMap.put(Integer.valueOf(key), value);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("载入渔获资料数量: " + _fishingMap.size() + "(" + timer.get() + "ms)");
  }
  
  private static void delete(int item_id) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `server_fishing` WHERE `itemid`=?");
      ps.setInt(1, item_id);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public L1Fishing get_fish(int fishingpoleid) {
    try {
      Collection<L1Fishing> fishlist = _fishingMap.values();
      int comparechance = 1000000;
      Iterator<L1Fishing> iterator = fishlist.iterator();
      while (iterator.hasNext()) {
        L1Fishing fish = iterator.next();
        if (fish.get_fishingpole() == fishingpoleid || fish.get_fishingpole2() == fishingpoleid) {
          int chance = fish.get_random();
          if (_random.nextInt(comparechance) < chance)
            return fish; 
          comparechance -= chance;
        } 
      } 
      return null;
    } catch (Exception ex) {
      return null;
    } 
  }
}
